package lv.bond.science.nnstudio.ui.testresult;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class TestResultView extends ViewPart {

	public static final String ID = 
		"lv.bond.science.nnstudio.ui.testresult.TestResultView";
	// Needs to be whatever is mentioned in plugin.xml

	private Composite top = null;
	private Table testResultTable = null;
	private static TableViewer tableViewer = null;
	private static List<TestResult> model = new ArrayList<TestResult>();  //  @jve:decl-index=0:
	private ArrayContentProvider contentProvider = null;  //  @jve:decl-index=0:
	private Label errorPercentLabel = null;
	private static Text errorPercentText = null;

	@Override
	public void createPartControl(Composite parent) {
		GridData gridData2 = new GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		gridData2.horizontalAlignment = GridData.BEGINNING;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = false;
		gridData1.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.widthHint = 220;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalSpan = 2;
		gridData.heightHint = 120;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		testResultTable = new Table(top, SWT.BORDER | SWT.MULTI);
		testResultTable.setHeaderVisible(true);
		testResultTable.setLayoutData(gridData);
		testResultTable.setLinesVisible(true);
		errorPercentLabel = new Label(top, SWT.RIGHT);
		errorPercentLabel.setText("Errors %");
		errorPercentLabel.setLayoutData(gridData1);
		errorPercentText = new Text(top, SWT.BORDER);
		errorPercentText.setEditable(false);
		errorPercentText.setLayoutData(gridData2);
		TableColumn tableColumn = new TableColumn(testResultTable, SWT.NONE);
		tableColumn.setWidth(170);
		tableColumn.setText("Classification - Winner Neuron");
		TableColumn tableColumn1 = new TableColumn(testResultTable, SWT.NONE);
		tableColumn1.setWidth(130);
		tableColumn1.setText("Target Winner Neuron");
		TableColumn tableColumn2 = new TableColumn(testResultTable, SWT.NONE);
		tableColumn2.setWidth(60);
		tableColumn2.setText("Match");
		tableViewer = new TableViewer(testResultTable);
		tableViewer.setLabelProvider(new TestResultLabelProvider());
		contentProvider = new ArrayContentProvider();
		tableViewer.setContentProvider(contentProvider);
		//model  = TestResult.example();
		tableViewer.setInput(model);
	}
	
	public static void addToTableRow() {
		int index = model.size()+1;
		TestResult tr = new TestResult(index, (index%2 == 0) ? index: index+1);
		addTestResult(tr);
	}
	
	public static void addTestResult(TestResult tr) {
		model.add(tr);
		tableViewer.add(tr);
		errorPercentText.setText(getErrorsPercent()+"");
	}

	@Override
	public void setFocus() {
		testResultTable.setFocus();
	}
	
	private static double getErrorsPercent() {
		int testsCount = model.size();
		int errorsCount = 0;
		for (Iterator iter = model.iterator(); iter.hasNext();) {
			TestResult tr = (TestResult) iter.next();
			if ( !tr.isMatching()) {
				errorsCount++;
			}
		}
		return ( (errorsCount  * 100) / testsCount );
	}

}
